<template>
  <div class="login-wrap">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  label-position="left" label-width="0px"
      class="demo-ruleForm login-container">
      <h3 class="title">用户注册</h3>
      <el-form-item label="" prop="name">
        <el-input type="text" autocomplete="off" v-model="ruleForm.username" prefix-icon="el-icon-user-solid"
        placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="" prop="name">
        <el-input type="text" autocomplete="off" v-model="ruleForm.nickname"
        placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="" prop="name">
        <el-input type="text" autocomplete="off" v-model="ruleForm.phone"
        placeholder="请输入手机号"></el-input>
      </el-form-item>
        <el-form-item label="" prop="pass">
          <el-input type="password" autocomplete="off" v-model="ruleForm.password" prefix-icon="el-icon-lock"
            placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="" prop="checkPass">
          <el-input type="password" autocomplete="off" v-model="ruleForm.checkPass" prefix-icon="el-icon-lock"
            placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>


</template>

<script>
import {register} from "../../api/jiangMG";
export default {
  name: "backUserRegister",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      activeName: "second",
      ruleForm: {
        username: "",
        password: "",
        checkPass: "",
        nickname: "",
        phone: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入您的名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        password: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  created() { },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this;
          register(this.ruleForm).then((res)=>{
            _this.$message({
              type: "success",
              message: "注册成功",
            });
            this.$router.replace({ path: "/login"});
          });
          // this.$message({
          //   type: "success",
          //   message: "注册成功",
          // });
          // this.activeName: 'first',

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
  .login-wrap {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    padding-top: 10%;
    /* background-color: #112346; */
    background-repeat: no-repeat;
    background-position: center right;
    background-size: 100%;
  }

.btns {
  display: flex;
  justify-content: flex-end;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.remember {
  margin: 0px 0px 35px 0px;
}

.code-box {
  text-align: right;
}

.codeimg {
  height: 40px;
}
</style>
